package com.cuit.riskassessment.action.client;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cuit.riskassessment.service.ClientService;
import com.cuit.riskassessment.util.Result;
import com.cuit.riskassessment.vo.Client;

@Controller
@RequestMapping(value = "clientAction")
public class ClientAction {
	@Resource(name = "clientServiceObject")
	private ClientService service;
	@RequestMapping(value = "getAllClients")
	public @ResponseBody List<Client> getAllClients(@RequestParam("name")String name,@RequestParam("val")String val){
		if (name==null||name=="") {
			System.out.println("查全部");
			return service.getAllClients();
		}
		return service.getClientByName(name,val);
	}
	@RequestMapping(value = "addOrUpdateClient")
	public @ResponseBody Object  addOrUpdateClient(HttpServletResponse response ,Client client,String id){
		response.setHeader("Accept", "application/json");
		String message = new String("操作成功");
		Result res = new Result();
		res.setSuccess(true);
		System.out.println(client.getId());
		if (client.getId() == null||client.getId() =="") {
			service.addOrUpdateClient(true, client);
			res.setSuccess(true);
			message = new String("添加成功");
		}else {
			message = new String("更新成功");
			service.addOrUpdateClient(false, client);
			res.setSuccess(true);
		}
		res.setMessage(message);
		return res;
	}
	@RequestMapping(value = "delClient")
	public @ResponseBody Object delClient(HttpServletResponse response ,String id){
		response.setHeader("Accept", "application/json");
		String message = new String("操作成功");
		Result res = new Result();
		System.out.println(id);
		service.delClient(Integer.valueOf(id));
		res.setMessage(message);
		return res;
	}
}
